package gameObject
{
	import assets.Asset;
	
	import events.LoadedEvent;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.net.Responder;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.text.TextField;
	import flash.text.TextFormat;
	import flash.utils.Dictionary;
	
	import flashx.textLayout.formats.TextAlign;
	
	public class User extends Sprite
	{
		private var _uid:int;
		private var _uname:String;
		private var _niname:String;
		private var _level:int;
		private var _exp:uint;
		private var _money:int;
		private var _vip:int;
		private var _titleID:int;
		private var _title:String;
		
		public var avi:int;
		public var acc:int;
		public var crit:int;
		
		private var _titleXML:XML;
		private var _titleLoader:URLLoader;
		private var _headIconURL:String = Global.assetPath+"UI/icon/head/"+"1"+".png";//现在数据库中还没有关于头像的记录，所以直接在前端取的1号头像
		
		private var _back:Bitmap;
		private var _infoHolder:Sprite;
		private var _headIcon:Bitmap;
		private var _nameText:TextField;
		private var _vipText:TextField;
		private var _levelText:TextField;
		private var _expText:TextField;
		private var _moneyText:TextField;
		
		///////////////////////////////////////////////////
		public function get vip():int
		{
			return _vip;
		}

		public function set vip(value:int):void
		{
			_vip = value;
			_vipText.text = "VIP:"+String(_vip);
		}

		public function get uname():String
		{
			return _uname;
		}
		
		public function get money():int
		{
			return _money;
		}

		public function set money(value:int):void
		{
			_money = value;
			_moneyText.text = "资金："+String(_money);
		}

		public function get exp():uint
		{
			return _exp;
		}

		public function set exp(value:uint):void
		{
			_exp = value;
			_expText.text = "经验："+String(_exp);
		}

		public function get level():int
		{
			return _level;
		}

		public function set level(value:int):void
		{
			_level = value;
			_levelText.text = "等级："+String(_level);
		}

		public function get title():int
		{
			return _titleID;
		}

		public function set title(value:int):void
		{
			_titleID = value;
			
			_title = _titleXML.children()[_titleID].@name;
			_nameText.text = _title+":"+_niname;
		}

		public function get niname():String
		{
			return _niname;
		}
		
		public function set niname(value:String):void
		{
			_niname = value;
			
			_nameText.text = _title+":"+_niname;
		}
		///////////////////////////////////////////////////
		public function User(UID:int)
		{
			_uid = UID;
			
			_infoHolder = new Sprite;
			_nameText = new TextField;
			_vipText = new TextField;
			_levelText = new TextField;
			_expText = new TextField;
			_moneyText = new TextField;
			_nameText.mouseEnabled = _vipText.mouseEnabled = _levelText.mouseEnabled = _expText.mouseEnabled = _moneyText.mouseEnabled = false;
			_nameText.textColor = _vipText.textColor = _levelText.textColor = _expText.textColor = _moneyText.textColor = 0xdddddd;
			
			var format:TextFormat = new TextFormat("Microsoft YaHei");
			format.align = TextAlign.LEFT;
			format.size = 16;
			_nameText.defaultTextFormat = format;
			_nameText.width = 400;
			_nameText.x = 103;
			_nameText.y = 1;
			
			_vipText.defaultTextFormat = format;
			_vipText.x = 280;
			_vipText.y = 1;
			
			format = new TextFormat("Microsoft YaHei");
			format.align = TextAlign.RIGHT;
			format.size = 16;
			_moneyText.defaultTextFormat = format;
			_moneyText.width = 200;
			_moneyText.x = Global.stage.stageWidth-380;
			_moneyText.y = 1;
			
			format = new TextFormat("Microsoft YaHei");
			format.align = TextAlign.LEFT;
			format.size = 10;
			_expText.defaultTextFormat = format;;
			_expText.x = 103;
			_expText.y = 25;
			
			format = new TextFormat("Microsoft YaHei");
			format.align = TextAlign.LEFT;
			format.size = 14;
			_levelText.defaultTextFormat = format;;
			_levelText.x = 75;
			_levelText.y = 56;
			
			Global.phpServer.call("userServer.getUser", new Responder(gotValues),_uid);
			addEventListener(Event.ADDED_TO_STAGE,onAdded);
		}
		
		public function dispayInfo(BitmapDatas:Dictionary):void
		{
			var bkBmd:BitmapData = BitmapDatas["userTie"];
			_back = new Bitmap(bkBmd);
			_back.scaleX = Global.stage.stageWidth;
			
			_infoHolder.addChild(_back);
			
			var uBmd:BitmapData = BitmapDatas["user"];
			var uBm:Bitmap = new Bitmap(uBmd);
			_infoHolder.addChild(uBm);
			
			addChild(_infoHolder);
			
			_infoHolder.addChild(_nameText);
			_infoHolder.addChild(_vipText);
			_infoHolder.addChild(_levelText);
			_infoHolder.addChild(_expText);
			_infoHolder.addChild(_moneyText);
			
			Asset.bitmapdata.addEventListener(LoadedEvent.BITMAPDATA_COMPLETE,gotHeadIcon);
			Asset.bitmapdata.load(_headIconURL);
		}
		
		private function gotHeadIcon(e:LoadedEvent):void
		{
			if(e.URL==_headIconURL)
			{
				Asset.bitmapdata.removeEventListener(LoadedEvent.BITMAPDATA_COMPLETE,gotHeadIcon);
				
				_headIcon = new Bitmap(Asset.resBitmapDatas[_headIconURL]);
				_headIcon.x = 4;
				_infoHolder.addChild(_headIcon);
			}
		}
		
		private function onAdded(e:Event):void
		{
			removeEventListener(Event.ADDED_TO_STAGE,onAdded);
			addEventListener(Event.REMOVED_FROM_STAGE,onRemoved);
			Global.stage.addEventListener(Event.RESIZE,onResize);
		}
		private function onRemoved(e:Event):void
		{
			removeEventListener(Event.REMOVED_FROM_STAGE,onRemoved);
			removeEventListener(Event.RESIZE,onResize);
		}
		private function onResize(e:Event):void
		{
			_back.scaleX = Global.stage.stageWidth;
			_moneyText.x = Global.stage.stageWidth-380;
		}
		
		///////////////////////////////////////////////////
		private function gotValues(obj:Object):void
		{
			_uname = obj.uname;
			_niname = obj.niname;
			_vip = obj.vip;
			_level = obj.level;
			_exp = obj.exp;
			_titleID = obj.title;
			_money = obj.money;
			avi = obj.avi;
			acc = obj.acc;
			crit = obj.crit;
			
			_titleLoader = new URLLoader;
			_titleLoader.addEventListener(Event.COMPLETE,titleLoaded);
			_titleLoader.load(new URLRequest(Global.iniAddress+"titles.xml"));
		}
		
		private function titleLoaded(e:Event):void
		{
			_titleLoader.removeEventListener(Event.COMPLETE,titleLoaded);
			_titleXML = new XML(_titleLoader.data);
			
			_title = _titleXML.children()[_titleID].@name;
			
			setupInfo();
			dispatchEvent(new Event(Event.COMPLETE));
		}
		
		private function setupInfo():void
		{
			_nameText.text = _title+":"+_niname;
			vip = _vip;
			level = _level;
			exp = _exp;
			money = _money;
			
		}
	}
}